package com.yanqu.road.server.channelserver.timer.xrank;

import com.yanqu.road.entity.enums.eSeasonRewardType;
import com.yanqu.road.entity.enums.eSystemId;
import com.yanqu.road.entity.rank.xrank.XSeasonStageUserRank;
import com.yanqu.road.entity.rank.xrank.XSeasonUserRank;
import com.yanqu.road.logic.pb.XRankPb;
import com.yanqu.road.logic.xlogic.rank.channel.XChannelRankMap;
import com.yanqu.road.logic.xlogic.rank.season.XSeasonUserRankListModel;
import com.yanqu.road.logic.xlogic.rank.season.XSeasonUserRankMap;
import com.yanqu.road.logic.xlogic.rank.season.stage.XSeasonStageUserRankListModel;
import com.yanqu.road.logic.xlogic.rank.season.stage.XSeasonStageUserRankMap;
import com.yanqu.road.logic.xlogic.rank.user.XUserRankMap;
import com.yanqu.road.utils.YanQuMessageUtils;
import com.yanqu.road.pb.cross3.activity.XRankProto;
import com.yanqu.road.server.channelserver.logic.ChannelMessageHelper;
import com.yanqu.road.server.channelserver.manager.server.ChannelRegisterServerMgr;
import com.yanqu.road.server.channelserver.manager.xrank.ChannelXRankMgr;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.Cross3Protocol;
import com.yanqu.road.utils.executor.GeneralTask;

import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class ChannelXRankTask extends GeneralTask {

    @Override
    public void doRun() {

        List<Integer> channelIdList = ChannelRegisterServerMgr.getChannelIdList();

        // 同步 个人榜 到跨服
        XUserRankMap userRankMap = ChannelXRankMgr.getUserRankMap();
        List<XRankProto.XUserRankListMsg.Builder> syncCache = userRankMap.getSyncCacheAndClear(true);
        for (XRankProto.XUserRankListMsg.Builder builder : syncCache) {

            // 每个渠道，同步非自己渠道的数据
            for (int channelId : channelIdList) {
                XRankProto.XUserRankListMsg.Builder tmpBuilder = XRankProto.XUserRankListMsg.newBuilder();
                tmpBuilder.setActivityId(builder.getActivityId());
                tmpBuilder.setConditionType(builder.getConditionType());
                for (XRankProto.XUserRankMsg rankMsg : builder.getRankListList()) {
                    if (rankMsg.getChannelId() != channelId) {
                        tmpBuilder.addRankList(rankMsg);
                    }
                }
                if (tmpBuilder.getRankListCount() == 0) {
                    continue;
                }

                // 分割后发送
                for (XRankProto.XUserRankListMsg.Builder cutBuilder : XRankPb.cutXUserRankList(tmpBuilder)) {
                    YanQuMessage message = YanQuMessageUtils.buildMessage(Cross3Protocol.C3_XRANK_SYNC_USER_RANK_FROM_CHANNEL, cutBuilder);
                    ChannelMessageHelper.sendToChannel(message, channelId);
                }
            }

        }

        // 同步 区域榜 个人贡献 到跨服
        XChannelRankMap channelRankMap = ChannelXRankMgr.getChannelRankMap();
        List<XRankProto.XChannelRankPartListMsg.Builder> cacheList2 = channelRankMap.getSyncPartCacheAndClear();
        for (XRankProto.XChannelRankPartListMsg.Builder builder : cacheList2) {

            // 每个渠道，同步非自己渠道的数据
            for (int channelId : channelIdList) {
                XRankProto.XChannelRankPartListMsg.Builder tmpBuilder = XRankProto.XChannelRankPartListMsg.newBuilder();
                tmpBuilder.setActivityId(builder.getActivityId());
                tmpBuilder.setConditionType(builder.getConditionType());
                for (XRankProto.XChannelRankPartMsg partMsg : builder.getPartMsgList()) {
                    if (partMsg.getChannelId() != channelId) {
                        tmpBuilder.addPartMsg(partMsg);
                    }
                }
                if (tmpBuilder.getPartMsgCount() == 0) {
                    continue;
                }

                // 分割后发送
                for (XRankProto.XChannelRankPartListMsg.Builder cutBuilder : XRankPb.cutXChannelPartList(tmpBuilder)) {
                    YanQuMessage pbMessage = YanQuMessageUtils.buildMessage(Cross3Protocol.C3_XRANK_SYNC_CHANNEL_RANK_PART_FROM_CHANNEL, cutBuilder);
                    ChannelMessageHelper.sendToChannel(pbMessage, channelId);
                }
            }

        }

        // 同步 赛季个人榜 到跨服
        XSeasonUserRankMap seasonUserRankMap = ChannelXRankMgr.getSeasonUserRankMap();
        List<XRankProto.XSeasonUserRankListMsg.Builder> syncSeasonCache = seasonUserRankMap.getSyncCacheAndClear(true);
        for (XRankProto.XSeasonUserRankListMsg.Builder builder : syncSeasonCache) {

            // 每个渠道，同步非自己渠道的数据
            for (int channelId : channelIdList) {
                XRankProto.XSeasonUserRankListMsg.Builder tmpBuilder = XRankProto.XSeasonUserRankListMsg.newBuilder();
                tmpBuilder.setSystemId(builder.getSystemId());
                tmpBuilder.setSeasonId(builder.getSeasonId());
                tmpBuilder.setType(builder.getType());
                tmpBuilder.setRangeKey(builder.getRangeKey());
                for (XRankProto.XUserRankMsg rankMsg : builder.getRankListList()) {
                    if (rankMsg.getChannelId() != channelId) {
                        tmpBuilder.addRankList(rankMsg);
                    }
                }
                if (tmpBuilder.getRankListCount() == 0) {
                    continue;
                }

                // 直接全部发送
                YanQuMessage message = YanQuMessageUtils.buildMessage(Cross3Protocol.C3_XRANK_SYNC_SEASON_USER_RANK_FROM_CHANNEL, tmpBuilder);
                ChannelMessageHelper.sendToChannel(message, channelId);
            }
        }

    }
}
